﻿Imports DotNetNuke
Imports DotNetNuke.Common
Imports DotNetNuke.Security
Imports DotNetNuke.Entities.Profile
Imports DotNetNuke.Entities.Users
Imports DotNetNuke.Entities.Users.UserProfile
Imports System.Web.UI
Imports Telerik.Web.UI
Imports DotNetNuke.Security.Membership
Imports DotNetNuke.Services.Mail

Namespace dnnWerk.Modules.Users

    Partial Class uc_Memberlist
        Inherits UserModuleBase


#Region "Event Handlers"

        Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init

            DotNetNuke.Framework.AJAX.RegisterScriptManager()

        End Sub

        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Try

                If Not Page.IsPostBack Then
                    BindUsers()
                End If

            Catch exc As Exception
                ProcessModuleLoadException(Me, exc)
            End Try
        End Sub

        Protected Sub rptUsers_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptUsers.ItemDataBound

            Dim strTemplate As String = ""
            Dim objUser As UserInfo = Nothing

            Select Case e.Item.ItemType
                Case ListItemType.Header

                    strTemplate = Template_UserListHeader(CurrentLocale)
                    ProcessContainerTemplate(strTemplate, e.Item.Controls)

                Case ListItemType.Item

                    objUser = CType(e.Item.DataItem, UserInfo)
                    strTemplate = Template_UserListItem(CurrentLocale)
                    ProcessItemTemplate(strTemplate, e.Item.Controls, objUser)

                Case ListItemType.AlternatingItem

                    objUser = CType(e.Item.DataItem, UserInfo)
                    strTemplate = Template_UserListAlternatingItem(CurrentLocale)
                    ProcessItemTemplate(strTemplate, e.Item.Controls, objUser)

                Case ListItemType.Footer

                    strTemplate = Template_UserListFooter(CurrentLocale)
                    ProcessContainerTemplate(strTemplate, e.Item.Controls)

            End Select

        End Sub

#End Region

#Region "Private Methods"

        Private Sub BindUsers()

            rptUsers.DataSource = GetResultList()
            rptUsers.DataBind()

        End Sub

        Private Function GetResultList() As List(Of UserInfo)

            Dim result As List(Of UserInfo) = Nothing
            Dim users As New ArrayList

            Dim blnUseCache As Boolean = True
            Dim strProperty As String = Null.NullString
            Dim strValue As String = Null.NullString


            If Not Request.QueryString("FilterBy") Is Nothing Then
                If Not Request.QueryString("Value") Is Nothing Then
                    If Not Request.QueryString("Value") = "All" Then

                        blnUseCache = False
                        strProperty = Request.QueryString("FilterBy")
                        strValue = Request.QueryString("Value")

                    End If
                End If
            End If


            'first: get userlist
            If Not DataCache.GetCache("dnnWerk_Memberlist_" & PortalId.ToString) Is Nothing Then
                users = CType(DataCache.GetCache("dnnWerk_Memberlist_" & PortalId.ToString), ArrayList)
            Else
                users = UserController.GetUsers(PortalId)
                DataCache.SetCache("dnnWerk_Memberlist_" & PortalId.ToString, users)
            End If

            'second: getfiltered list
            If blnUseCache Then
                If Not DataCache.GetCache("dnnWerk_Resultlist_" & PortalId.ToString) Is Nothing Then
                    result = CType(DataCache.GetCache("dnnWerk_Resultlist_" & PortalId.ToString), List(Of UserInfo))
                    If Not result Is Nothing Then

                        Return result

                    End If
                End If
            End If

            result = New List(Of UserInfo)

            For Each objUser As UserInfo In users

                Dim blnAdd As Boolean = True

                If blnAdd Then 'user is allowed for list so far

                    If Not BrowseRoles Is Nothing AndAlso BrowseRoles.Length > 0 Then 'we must fiter the list for role membership
                        Dim rc As New DotNetNuke.Security.Roles.RoleController
                        For Each roleid As String In BrowseRoles
                            If roleid.Length > 0 Then
                                Dim role As DotNetNuke.Security.Roles.RoleInfo = rc.GetRole(Convert.ToInt32(roleid), PortalId)
                                If Not role Is Nothing Then

                                    blnAdd = objUser.IsInRole(role.RoleName)

                                    If blnAdd = False Then 'user is not in one of the filterby roles, skip other roles
                                        Exit For
                                    End If

                                End If
                            End If
                        Next
                    End If

                End If


                If blnAdd Then 'user is allowed for list so far

                    If String.IsNullOrEmpty(strProperty) = False AndAlso String.IsNullOrEmpty(strValue) = False Then 'we must fiter the list for property attributes

                        Try
                            Dim strProfileValue As String = objUser.Profile.GetPropertyValue(strProperty)
                            If strProfileValue.ToLower.StartsWith(strValue.ToLower) = False Then
                                blnAdd = False
                            End If
                        Catch
                        End Try

                    End If

                End If

                If blnAdd Then
                    result.Add(objUser)
                End If

            Next

            If blnUseCache Then
                DataCache.SetCache("dnnWerk_Resultlist_" & PortalId.ToString, result)
            End If

            Return result

        End Function

        Private Sub ProcessItemTemplate(ByVal strTemplate As String, ByRef controls As ControlCollection, ByVal objUser As UserInfo)
            ProcessDisplayTemplate(controls, objUser, strTemplate)
        End Sub

        Private Sub ProcessContainerTemplate(ByVal strTemplate As String, ByRef controls As ControlCollection)

            Dim literal As New Literal
            Dim delimStr As String = "[]"
            Dim delimiter As Char() = delimStr.ToCharArray()

            Dim templateArray As String()
            templateArray = strTemplate.Split(delimiter)

            For iPtr As Integer = 0 To templateArray.Length - 1 Step 2

                Dim strHTML As String = templateArray(iPtr).ToString()
                controls.Add(New LiteralControl(strHTML))

                If iPtr < templateArray.Length - 1 Then

                    Dim strToken As String = templateArray(iPtr + 1)
                    Select Case strToken

                        Case ""
                            'Try
                            '    Dim objLiteral As New Literal
                            '    objLiteral.Text = ""
                            '    objLiteral.EnableViewState = False
                            '    controls.Add(objLiteral)
                            'Catch
                            'End Try

                    End Select
                End If
            Next
        End Sub

#End Region

        
    End Class

End Namespace
